package com.prabhakar.json;

import java.io.IOException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class ConsumeJson_Jackson {

	public void consumeJsonArray(JSONArray jsonArray){

		System.out.println("jsonArray recevd in ConsumeJson_Jackson >> \n"+jsonArray);

		for(int i=0; i< jsonArray.size() ;i++){

			JSONObject jsonObj = jsonArray.getJSONObject(i);
		//	System.out.println("\n jsonObj.toString() from consumeJsonArray "+jsonObj.toString());
			
			DemoBean beanObj = jsonToJava(jsonObj.toString());
			
			printBeanObjValues(beanObj);
		}
	}

	private DemoBean jsonToJava(String jsonObj) {

		DemoBean beanObj = null;
		try {
			System.out.println(" ## JsonObj to be parsed >>> "+jsonObj);
			ObjectMapper objectMapper = new ObjectMapper();
		//	objectMapper.configure(Des, state)
			
			beanObj = new ObjectMapper().readValue(jsonObj, DemoBean.class);
				System.out.println(" ## beanObj parsed >>> "+beanObj.getName());
			
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return beanObj;
	}



	private void printBeanObjValues(DemoBean beanObj) {
		
		if(beanObj!=null){
			System.out.println("\n-----------------------------------------------");
			System.out.println(" name retrieved from bean >>> "+beanObj.getName());
			System.out.println(" phonenumber retrieved from bean >>> "+beanObj.getPhonenumber());
			System.out.println(" email retrieved from bean >>> "+beanObj.getEmail());
			System.out.println(" address retrieved from bean >>> "+beanObj.getAddress());
			System.out.println(" date retrieved from bean >>> "+beanObj.getRequestDate());
			System.out.println("\n-----------------------------------------------");
		}
	}
	
}










/*
 * 									-========LOGS Shared===============
 * 
 * 
 * 	json obj.to string  >> {"name":"prabhakar1","phonenumber":"74859650011","requestDate":"12-09-2013","email":"prabhakar1@mail.com","address":"1st cross, 16th main, btm 2nd stage"}
		json obj.to string  >> {"name":"prabhakar2","phonenumber":"74859650022","requestDate":"12-09-2013","email":"prabhakar2@mail.com","address":"1st cross, 16th main, btm 2nd stage"}
		json obj.to string  >> {"name":"prabhakar3","phonenumber":"74859650033","requestDate":"12-09-2013","email":"prabhakar3@mail.com","address":"1st cross, 16th main, btm 2nd stage"}
		jsonArray recevd in ConsumeJson_Jackson >> 
		[{"name":"prabhakar1","phonenumber":"74859650011","requestDate":"12-09-2013","email":"prabhakar1@mail.com","address":"1st cross, 16th main, btm 2nd stage"},{"name":"prabhakar2","phonenumber":"74859650022","requestDate":"12-09-2013","email":"prabhakar2@mail.com","address":"1st cross, 16th main, btm 2nd stage"},{"name":"prabhakar3","phonenumber":"74859650033","requestDate":"12-09-2013","email":"prabhakar3@mail.com","address":"1st cross, 16th main, btm 2nd stage"}]
		 ## JsonObj to be parsed >>> {"name":"prabhakar1","phonenumber":"74859650011","requestDate":"12-09-2013","email":"prabhakar1@mail.com","address":"1st cross, 16th main, btm 2nd stage"}
		 ## beanObj parsed >>> prabhakar1

		-----------------------------------------------
		 name retrieved from bean >>> prabhakar1
		 phonenumber retrieved from bean >>> 74859650011
		 email retrieved from bean >>> prabhakar1@mail.com
		 address retrieved from bean >>> 1st cross, 16th main, btm 2nd stage
		 date retrieved from bean >>> 12-09-2013

		-----------------------------------------------
		 ## JsonObj to be parsed >>> {"name":"prabhakar2","phonenumber":"74859650022","requestDate":"12-09-2013","email":"prabhakar2@mail.com","address":"1st cross, 16th main, btm 2nd stage"}
		 ## beanObj parsed >>> prabhakar2

		-----------------------------------------------
		 name retrieved from bean >>> prabhakar2
		 phonenumber retrieved from bean >>> 74859650022
		 email retrieved from bean >>> prabhakar2@mail.com
		 address retrieved from bean >>> 1st cross, 16th main, btm 2nd stage
		 date retrieved from bean >>> 12-09-2013

		-----------------------------------------------
		 ## JsonObj to be parsed >>> {"name":"prabhakar3","phonenumber":"74859650033","requestDate":"12-09-2013","email":"prabhakar3@mail.com","address":"1st cross, 16th main, btm 2nd stage"}
		 ## beanObj parsed >>> prabhakar3

		-----------------------------------------------
		 name retrieved from bean >>> prabhakar3
		 phonenumber retrieved from bean >>> 74859650033
		 email retrieved from bean >>> prabhakar3@mail.com
		 address retrieved from bean >>> 1st cross, 16th main, btm 2nd stage
		 date retrieved from bean >>> 12-09-2013

		-----------------------------------------------
		*
		*
		*/





